import {computed, reactive, ref} from "vue";
import {getClassList,getClassListNew} from "../../api/classes";
import {FetchKXClassListDAT} from "../../types/responseTypes";
import {useStore} from "vuex";

export function useClasses(){
    // const classList = reactive<FetchKXClassListDAT[]>([])
    const classListWrap = reactive<{classList: FetchKXClassListDAT[]}>({
        classList: []
    })
    const total = ref(0)
    const {state} = useStore()
    const OPERATOR_ID = state.user.userInfo.CUSER_ID || 0
    function initClassList(page: number = 1,pageSize: number = 20,CLASS_IDS: string){
        return new Promise((resolve, reject) => {
            getClassList({
                OPERATOR_ID,
                PAGEINDEX: page,
                PAGESIZE: 2000,
                CLASS_IDS,
            }).then(res => {
                if (res.ERROR[0].errcode == '0'){
                    total.value = res.COUNT[0].NUM
                    if (res.DAT.length == 0){
                        reject('nomore')
                    }
                    if (classListWrap.classList.length === 0){
                        classListWrap.classList.push(...res.ZL,...res.DAT)
                    }else {
                        const arr = [...classListWrap.classList,...res.ZL,...res.DAT]
                        const set = new Set<number>(arr.map(item => item.CLASS_ID));
                        classListWrap.classList = [...set].map(id => (arr.find(item => item.CLASS_ID === id) as FetchKXClassListDAT))
                    }
                    resolve(null)
                }else {
                    uni.showToast({
                        title: res.ERROR[0].error,
                        icon: 'none',
                        mask: true
                    })
                    reject('err')
                }
            }).catch(err => {
                uni.showToast({
                    title: '获取列表失败',
                    icon: 'error',
                    mask: true
                })
                reject('err')
            })
        })
    }

    return {
        classList: computed(() => classListWrap.classList),
        total,
        initClassList
    }
}


export function useClassesNew(){
    const classListWrap = reactive<{classList: FetchKXClassListDAT[]}>({
        classList: []
    })
    const total = ref(0)
    const {state} = useStore()
    
    // const OPERATOR_ID = uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').CUSER_ID : 0
    function initClassList(page: number = 1,pageSize: number = 20,CLASS_IDS: string, idx?: string | number){
        return new Promise((resolve, reject) => {
            getClassListNew({
                OPERATOR_ID: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').CUSER_ID : 0,
                PAGEINDEX: page,
                PAGESIZE: 2000,
                CLASS_IDS,
            }).then(res => {
                if (res.ERROR[0].errcode == '0'){
                    total.value = res.COUNT[0].NUM

                    if (res.DAT.length == 0){
                        reject('nomore')
                    }

                    if (classListWrap.classList.length === 0){
                        classListWrap.classList.push(...res.DAT)
                    }else {
                        const arr = [...classListWrap.classList,...res.DAT]
                        const set = new Set<number>(arr.map(item => item.CLASS_ID));
                        classListWrap.classList = [...set].map(id => (arr.find(item => item.CLASS_ID === id) as FetchKXClassListDAT))
                    }
                    if(idx){
                        classListWrap.classList.filter((v)=> v.MAJOR_ID == idx)
                    }

                    resolve(null)
                }else {
                    uni.showToast({
                        title: res.ERROR[0].error,
                        icon: 'none',
                        mask: true
                    })
                    reject('err')
                }
            }).catch(err => {
                uni.showToast({
                    title: '获取列表失败',
                    icon: 'error',
                    mask: true
                })
                reject('err')
            })
        })
    }

    return {
        classList: computed(() => classListWrap.classList),
        total,
        initClassList
    }
}
